package com.sscl.easyble.extensions

import android.bluetooth.BluetoothAdapter
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanResult
import com.sscl.easyble.AdvertiseStruct
import java.util.UUID

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * 公开方法
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/**
 * 将扫描失败的错误码转换为字符串
 */
fun Int.getFailMsg(isZh: Boolean = true): String {
    return if (isZh) {
        getZhFailMsg()
    } else {
        getEnFailMsg()
    }
}

/**
 * 判断蓝牙MAC地址是否合法
 */
fun String.isValidBluetoothAddress(): Boolean {
    return BluetoothAdapter.checkBluetoothAddress(this)
}

/**
 * 判断是否扫描结果列表中是否包含某个扫描结果
 */
fun Collection<ScanResult>.containsScanResults(scanResult: ScanResult): Boolean {
    val device = scanResult.device
    for (result in this) {
        if (result.device.equals(device)) {
            return true
        }
    }
    return false
}

/**
 * 获取扫描结果列表中包含的某个扫描结果的索引 -1表示扫描结果列表中不存在该扫描结果
 */
fun Collection<ScanResult>.indexOfScanResults(scanResult: ScanResult): Int {
    val device = scanResult.device
    var index = 0
    var contains = false
    for (result in this) {
        if (result.device.equals(device)) {
            contains = true
            break
        }
        index++
    }
    return if (contains) {
        index
    } else {
        -1
    }
}

/**
 * 将AD结构数据转换为字节数组
 */
fun AdvertiseStruct?.toByteArray(): ByteArray? {
    this ?: return null
    val byteArray = ByteArray(length + 1)
    byteArray[0] = length.toByte()
    byteArray[1] = type
    for (i in 2 until byteArray.size) {
        byteArray[i] = data[i - 2]
    }
    return byteArray
}


fun String.toBluetoothGattFullUUID(): UUID {
    return when (this.length) {
        4 -> {
            UUID.fromString("0000$this-0000-1000-8000-00805f9b34fb")
        }

        8 -> {
            UUID.fromString("$this-0000-1000-8000-00805f9b34fb")
        }

        else -> {
            throw IllegalArgumentException("仅支持16位短UUID与32位UUID短")
        }
    }
}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * 私有方法
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

private fun Int.getZhFailMsg(): String {
    return when (this) {
        ScanCallback.SCAN_FAILED_ALREADY_STARTED -> {
            "扫描已经开启"
        }

        ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED -> {
            "扫描器注册失败"
        }

        ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED -> {
            "有不支持的扫描参数或特性"
        }

        ScanCallback.SCAN_FAILED_INTERNAL_ERROR -> {
            "内部错误"
        }

        else -> {
            "未知的错误 $this,请查看 android.bluetooth.le.ScanCallback 源码获取对应的错误信息"
        }
    }
}

fun Int.getEnFailMsg(): String {
    return when (this) {
        ScanCallback.SCAN_FAILED_ALREADY_STARTED -> {
            "SCAN_FAILED_ALREADY_STARTED"
        }

        ScanCallback.SCAN_FAILED_APPLICATION_REGISTRATION_FAILED -> {
            "SCAN_FAILED_APPLICATION_REGISTRATION_FAILED"
        }

        ScanCallback.SCAN_FAILED_FEATURE_UNSUPPORTED -> {
            "SCAN_FAILED_FEATURE_UNSUPPORTED"
        }

        ScanCallback.SCAN_FAILED_INTERNAL_ERROR -> {
            "SCAN_FAILED_INTERNAL_ERROR"
        }

        else -> {
            "unknown error $this,please check source code from \"android.bluetooth.le.ScanCallback\" to get more help."
        }
    }
}